<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $id
 * @property string $login
 * @property string $password
 * @property string $email
 * @property string $name
 * @property string $lastname
 * @property string $telephone
 * @property integer $roleId
 * @property integer $status
 * @property integer $activity
 * @property string $dataRegister
 * @property string $lastLoginTime
 */
class User extends CActiveRecord
{
    public $retypePassword;
    public $verifyCode;
    public $remember;

	public function tableName()
	{
		return 'user';
	}

	public function rules()
	{
		return array(
            array('role, status, activity', 'numerical', 'integerOnly'=>true, 'on'=>'insert,update'),
			array('login, name, lastname, telephone', 'length', 'max'=>32, 'on'=>'insert,update,register'),
            array('name', 'required', 'message'=>Yii::t('account/customer','errorFirstName'), 'on'=>'register'),
            array('login', 'required', 'message'=>Yii::t('account/customer','errorNoLogin'), 'on'=>'register'),
            array('email', 'required', 'message'=>Yii::t('account/customer','errorNoEmail'), 'on'=>'register'),
            array('login', 'unique', 'message'=>Yii::t('account/customer','errorLoginUnique'), 'on'=>'insert,update,register'),
			array('email', 'email', 'message'=>Yii::t('account/customer','errorEmail'), 'on'=>'login,insert,update,register'),
            array('email', 'unique', 'message'=>Yii::t('account/customer','errorEmailUnique'), 'on'=>'insert,update,register'),
            array('password', 'required', 'message'=>Yii::t('account/customer','errorPasswordMin'), 'on'=>'login,insert,update,register'),
			array('password', 'length', 'max'=>40,  'message'=>Yii::t('account/customer','errorPasswordMax'), 'on'=>'login,insert,update,register'),
            array('password', 'length', 'min'=>4, 'message'=>Yii::t('account/customer','errorPasswordMin'), 'on'=>'login,insert,update,register'),
            array('retypePassword','compare','compareAttribute'=>'password','message'=>Yii::t('account/customer','errorRetypePassword'),'on'=>'register'),
			array('dateRegister', 'safe', 'on'=>'insert,update'),
            array('verifyCode', 'captcha', 'allowEmpty'=>!Yii::app()->user->isGuest || !CCaptcha::checkRequirements(),
                'message'=>Yii::t('account/customer','errorСaptcha'),'on'=>'register'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
            array('id, login, password, email, name, lastname, telephone, role, status, activity, dataRegister, lastLoginTime', 'safe', 'on'=>'search'),

            /*
			array('login, password, email, dataRegister, remember', 'required'),
			array('role, status, activity, remember', 'numerical', 'integerOnly'=>true),
			array('login, name, lastname, telephone, address', 'length', 'max'=>128),
			array('password, email', 'length', 'max'=>256),
			array('lastLoginTime', 'safe'),
            array('verifyCode', 'captcha', 'allowEmpty'=>!Yii::app()->user->isGuest || !CCaptcha::checkRequirements(),
                'message'=>Yii::t('account/customer','errorСaptcha'),'on'=>'register'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, login, password, email, name, lastname, telephone, address, role, status, activity, dataRegister, lastLoginTime', 'safe', 'on'=>'search'),
            */
		);
	}

	public function relations()
	{
		return array(
            //'profile'=>array(self::HAS_ONE, 'Profile', 'userId'),
		);
	}

	public function attributeLabels()
	{
		return array(
			'id' => Yii::t('main','id'),
			'login' => Yii::t('account/customer','login'),
			'password' => Yii::t('account/customer','password'),
            'retypePassword'=>Yii::t('account/customer','retypePassword'),
			'email' => Yii::t('main','email'),
			'name' => Yii::t('main','name'),
			'lastname' => Yii::t('account/customer','lastname'),
			'telephone' => Yii::t('main','telephone'),
			'role' => Yii::t('main','role'),
			'status' => Yii::t('main','status'),
            'remember' => Yii::t('account/customer','remember'),
			'activity' => Yii::t('main','activity'),
			'dateRegister' => Yii::t('account/customer','dateRegister'),
			'lastLoginTime' => Yii::t('account/customer','lastLoginTime'),
            'verifyCode'    => Yii::t('main','verifyCode'),
            'confirmCode'   => Yii::t('account/customer','confirmCode'),
		);
	}

    public function safeAttributes()
    {
        return array(
            'id',
            'login',
            'name',
            'email',
            'password',
            'retypePassword',
            'confirmCode',
            'dateRegister',
            'role'
        );
    }

    public function getLoginBlockTitle()
    {
        return Setting::getConfig('login_block_title');
    }

    public function getImage()
    {
        return Yii::app()->url->theme.'/image/account/noavatar.png';
    }

	/**
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('login',$this->login,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('lastname',$this->lastname,true);
		$criteria->compare('telephone',$this->telephone,true);
		$criteria->compare('role',$this->role);
		$criteria->compare('status',$this->status);
		$criteria->compare('activity',$this->activity);
		$criteria->compare('dateRegister',$this->dataRegister,true);
		$criteria->compare('lastLoginTime',$this->lastLoginTime,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    public function beforeSave()
    {
        if($this->isNewRecord){
            $this->password=hash('md5', $this->email.$this->password.Yii::app()->params['salt']);
            $this->dateRegister=date('Y-m-d');
        }
        return parent::beforeSave();
    }

	/**
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
